// pages/search one/search one.js

Page({
    data: {
      list:[]  ,
      pictureLink: [
        {
        picture:'',
        intro:'',
        price:'',
        categoryId:'',
        }
    ],
      proname:'',
      display:'display:none;'
    },
    queryPro(e){
        var that = this;
            wx.request({
                url: 'http://localhost:8080/product/search?name='+e.detail.value,
                method:'POST',
                header:{
                    'content-type':'application/json'
                },
                success:function(res) {
                    that.setData({
                        pictureLink:res.data.data,
                        display:''
                    });
                },
                fail:function() {
                    console.log("失败");
                }
            })
    },
    
    //点击完成按钮时触发
    confirm(e){
      var that = this;
      that.setData({
          proname:e.detail.value
      })
    },
    search(key){
      var that=this;
      //从本地缓存中异步获取指定 key 的内容
      var list=wx.getStorage({
        key: 'list',
        //从Storage中取出存储的数据
        success: function(res) {
          // console.log(res)
          if(key==''){   //用户没有输入时全部显示
            that.setData({
              list:res.data
            })
            return;
          }
          var arr=[];     //临时数组，用于存放匹配到的数组
          for(let i in res.data){
            res.data[i].show=false;  //所有数据隐藏
            if (res.data[i].search.indexOf(key)>=0){
              res.data[i].show = true;  //让匹配到的数据显示
              arr.push(res.data[i])
            }
          }
          if(arr.length==0){
            that.setData({
              list:[{show:true,name:'没有相关数据！'}]
            })
          }else{
            that.setData({
              list: arr
            })
          }
        },
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      var list=[
        {name: "浅蓝色微喇叭牛仔裤", show: true, search:"女装"},
        {name: "POLO衫", show: true, search:"女装"},
        {name: "超级真Q软糖", show: true, search: "食品" },
        {name: "趣多多", show: true, search:"食品"},
      ]
      wx.setStorage({
        key: 'list',
        data: list
      })
      this.setData({
        list:list
      })
    },
  })
  